/**
 * 消费明细服务类
 * 业务层级：业务逻辑层
 * 功能说明：提供消费明细相关的业务逻辑处理，包括消费记录查询、筛选、统计和格式化
 * 架构关系：遵循四层架构设计，调用DAO层获取数据，为表示层提供业务服务
 */
#ifndef CONSUMPTIONSERVICE_H
#define CONSUMPTIONSERVICE_H

#include "../dao/consumptiondao.h"
#include "../database/database.h"
#include <QString>
#include <QMap>
#include <QVariant>
#include <QVector>
#include <QDateTime>

class ConsumptionService
{
public:
    explicit ConsumptionService(Database *db);
    ~ConsumptionService();

    // 获取格式化的会员消费记录
    QVector<QMap<QString, QVariant>> getFormattedConsumptionByMember(int memberId);

    // 获取格式化的等级消费记录
    QVector<QMap<QString, QVariant>> getFormattedConsumptionByLevel(int levelId);

    // 获取所有消费记录（用于界面展示）
    QVector<QMap<QString, QVariant>> getAllConsumptionForDisplay();

    // 根据时间范围获取消费记录
    QVector<QMap<QString, QVariant>> getConsumptionByDateRange(const QDateTime &startDate, const QDateTime &endDate);

    // 获取会员消费统计信息
    QMap<QString, QVariant> getMemberConsumptionSummary(int memberId);

    // 获取等级消费统计信息
    QVector<QMap<QString, QVariant>> getLevelConsumptionStats();

    // 获取订单详细信息
    QVector<QMap<QString, QVariant>> getOrderDetails(int orderId);

    // 筛选功能
    QVector<QMap<QString, QVariant>> filterConsumptionByAmount(const QVector<QMap<QString, QVariant>> &records, double minAmount, double maxAmount);
    QVector<QMap<QString, QVariant>> filterConsumptionByMember(const QVector<QMap<QString, QVariant>> &records, int memberId);
    QVector<QMap<QString, QVariant>> filterConsumptionByLevel(const QVector<QMap<QString, QVariant>> &records, int levelId);
    QVector<QMap<QString, QVariant>> filterConsumptionByStatus(const QVector<QMap<QString, QVariant>> &records, int status);
    QVector<QMap<QString, QVariant>> filterConsumptionByPaymentMethod(const QVector<QMap<QString, QVariant>> &records, const QString &methodName);

    // 排序功能
    QVector<QMap<QString, QVariant>> sortConsumptionByTime(const QVector<QMap<QString, QVariant>> &records, bool ascending = false);
    QVector<QMap<QString, QVariant>> sortConsumptionByAmount(const QVector<QMap<QString, QVariant>> &records, bool ascending = false);

    // 格式化函数
    QString formatAmount(double amount);
    QString formatDateTime(const QVariant &dateTime);
    QString formatOrderStatus(int status);
    QString formatPaymentMethod(const QString &methodName);
    QString formatDiscountInfo(double totalAmount, double discountAmount);

    // 验证函数
    bool isValidMemberId(int memberId);
    bool isValidOrderId(int orderId);
    bool isValidLevelId(int levelId);

private:
    ConsumptionDAO *m_consumptionDAO;
    Database *m_db;

    // 私有辅助函数
    void enhanceConsumptionRecord(QMap<QString, QVariant> &record);
    QString calculateSavingsText(double totalAmount, double discountAmount);
    QString getStatusColorCode(int status);
    QString getLevelDisplayName(int levelId);
};

#endif // CONSUMPTIONSERVICE_H